/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.netease.nim.demo.session.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.shwread.android.qysw.activity.BaseSimpleActivity;
import com.shwread.android.qysw10000204.R;
import com.shwread.android.ui.dialog.ClearChatRecordDialog;
import com.shwread.android.ui.dialog.SimpleListener;

import logic.util.Utils;


/**
 * @Project： qysw_1.0.8_dev
 * @Title：P2PInfoActivity
 * @Description：
 * @Author：wayne
 * @Date：2015年6月1日上午10:41:09
 */
public class P2PInfoActivity
        extends
        BaseSimpleActivity
        implements
        OnClickListener
{
	public static final int     P2PREQUEST_CODE           = 0x01;
	
	private static final String EXTRA_ID                  = "EXTRA_ID";
	
	public static final String  RESULT_EXTRA_REASON       = "RESULT_EXTRA_REASON";
	
	public static final String  RESULT_EXTRA_REASON_CLEAR = "RESULT_EXTRA_REASON_CLEAR";
	
	private TextView            titleTextView;
	private Button              backBtn;
	
	private View                queryView;
	private View                clearView;
	private String              receiverid;
	
	/**
	 * 聊天设置
	 * 
	 * @param context
	 *            调用方Activity
	 * @param id
	 *            讨论组ID
	 */
	public static void startForTeamInfo(Context context, String id) {
		start(context, id);
	}
	
	private static void start(Context context, String id) {
		Intent intent = new Intent();
		if (!TextUtils.isEmpty(id))
		{
			intent.putExtra(EXTRA_ID, id);
		}
		intent.setClass(context, P2PInfoActivity.class);
		((Activity) context).startActivityForResult(intent, P2PREQUEST_CODE);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.p2pinfo_lay);
		
		findViews();
		
		receiverid = getIntent().getStringExtra(EXTRA_ID);
	}
	
	void findViews() {
		titleTextView = (TextView) findViewById(R.id.common_header_name);
		titleTextView.setText(getString(R.string.team_info_title));
		backBtn = (Button) findViewById(R.id.common_header_back);
		backBtn.setVisibility(View.VISIBLE);
		backBtn.setOnClickListener(this);
		
		queryView = findViewById(R.id.p2pinfo_query_layout);
		queryView.setOnClickListener(this);
		clearView = findViewById(R.id.p2pinfo_clear_layout);
		clearView.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				onBackPressed();
				break;
			case R.id.p2pinfo_query_layout:
				RoamingMessageActivity.start(this, receiverid, SessionTypeEnum.P2P);
				break;
			case R.id.p2pinfo_clear_layout:
				clearHistory();
				break;
			default:
				break;
		}
	}
	
	@Override
	public void onBackPressed() {
		if (hasClearHistory)
		{
			setResult(Activity.RESULT_OK, new Intent().putExtra(RESULT_EXTRA_REASON, RESULT_EXTRA_REASON_CLEAR));
		}
		super.onBackPressed();
	}
	
	private boolean hasClearHistory;
	
	private void clearHistory() {
		new ClearChatRecordDialog(this, new SimpleListener()
		{
			@Override
			public void ok() {
				// 删除与某个聊天对象的全部消息记录
				NIMClient.getService(MsgService.class).clearChattingHistory(receiverid, SessionTypeEnum.P2P);
				Utils.showToast(P2PInfoActivity.this, "已清空聊天记录");
				hasClearHistory = true;
			}
		}).show();
	}
	
}
